package mess;

public class LeeCode_1144 {
    public static void main(String[] args) {
        System.out.println(movesToMakeZigzag(new int[]{9,10,7,1,7,6,5,5}));
    }

    private static int movesToMakeZigzag(int[] nums) {
        int len = nums.length;
        if (len == 1)
            return 0;
        int count0 = 0;
        int count1 = 0;
        for(int i = 0; i < len; i++){
            if(i % 2 == 1){
                if(i != len - 1){
                    int min = Math.min(nums[i - 1], nums[i + 1]);
                    count1 += nums[i] >= min ? nums[i] - min + 1 : 0;
                }else{
                    count1 += nums[i] >= nums[i - 1] ? nums[i] - nums[i - 1] + 1 : 0;
                }
            }

            if(i % 2 == 0){
                if(i == 0 ){
                    count0 += nums[i] >= nums[i + 1] ? nums[i] - nums[i + 1] + 1 : 0;
                    continue;
                }
                if(i != len - 1){
                    int min = Math.min(nums[i - 1], nums[i + 1]);
                    count0 += nums[i] >= min ? nums[i] - min + 1 : 0;
                }else {
                    count0 += nums[i] > nums[i - 1] ? nums[i] - nums[i - 1] + 1 : 0;
                }
            }
        }

        return Math.min(count0, count1);
    }
}
